//
//  ZJParticipateActivitiesViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/9/18.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJParticipateActivitiesViewController.h"
/** 地图 */
#import "QMapKit.h"
#import "ZJMapControlView.h"
#import "ZJPointAnnotation.h"
#import "ZJBookAndJobAnnotation.h"
#import "ActivityJoinDetailModel.h"
#import "LPActivityDetailViewController.h"
#import "BranchPartyOrgizationModel.h"
#import "BranchOpenPartyOrganizationViewController.h"
#import "ZJPAActionesTableView.h"
@interface ZJParticipateActivitiesViewController ()<QMapViewDelegate,ZJMapControlViewDelegate,ZJPAActionesTableViewDelegate>
{
    BOOL _isClickLocation;/** 是不是点击定位 */
    CLLocationCoordinate2D  CenterCoordinate;/** 记录当前位置 */
}

/**  分享通列表  */
@property (strong, nonatomic) ZJPAActionesTableView  *shareList;
@property (nonatomic, strong) QMapView *mapView;
/** 控制视图 */
@property (nonatomic, strong) ZJMapControlView *mapControlView;
/** 标记点数组 */
@property (nonatomic, strong) NSMutableArray *annotationArray;
@end

@implementation ZJParticipateActivitiesViewController

//地图将会显示的时候必须调用
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.mapView viewWillAppear];

    [self networking_atcit];
       
    [self.mapControlView setUpHomeAndWorkAddressLocation];
}

//地图不再显示的时候必须调用
- (void)viewDidDisappear:(BOOL)animated {
    [self.mapView viewDidDisappear];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"群团活动";
    _isClickLocation = NO;
    [self.view addSubview:self.mapView];
    [self.view addSubview:self.mapControlView];
    [self addUserBookAndJobMarkPoint];
    [self.mapControlView setUpHomeAndWorkAddressLocation];
    //    31.226894,121.497116 上海市坐标
    [self.mapView setCenterCoordinate:DEFLocation animated:YES];
    [self networking_atcit];
    [self.view addSubview:self.shareList];
    self.shareList.hidden = YES;
    
    //显示列表
    self.statusVeiw.hidden = NO;
    self.shareList.alpha = 0;
    self.shareList.hidden = NO;
    self.statusVeiw.alpha = 1;
    self.mapControlView.alpha = 0;
    self.mapView.alpha = 0;
    self.shareList.alpha = 1;
    self.mapControlView.hidden = YES;
    self.mapView.hidden = YES;
    [self.shareList initDataSoure];
   
}


#pragma mark - 添加在职在册的坐标点
- (void)addUserBookAndJobMarkPoint{
    if (ZJUSERINFORMODEL.DEPT_MODEL_JOB.DEPT_DEADDRESS_COORDINATE_X.length > 2 && ZJUSERINFORMODEL.DEPT_MODEL_JOB.DEPT_DEADDRESS_COORDINATE_Y.length > 2) {
        ZJBookAndJobAnnotation *annotation = [[ZJBookAndJobAnnotation alloc] init];
        annotation.coordinate = CLLocationCoordinate2DMake([ZJUSERINFORMODEL.DEPT_MODEL_JOB.DEPT_DEADDRESS_COORDINATE_X doubleValue], [ZJUSERINFORMODEL.DEPT_MODEL_JOB.DEPT_DEADDRESS_COORDINATE_Y doubleValue]);
        annotation.index = 1;
        [self.mapView addAnnotation:annotation];
    }else{
        self.mapControlView.homeButton.hidden = YES;
        self.mapControlView.workAddress.frame = self.mapControlView.homeButton.frame;
        self.mapControlView.homeButton.frame = CGRectZero;
    }
    if (ZJUSERINFORMODEL.DEPT_MODEL_BOOK.DEPT_DEADDRESS_COORDINATE_X.length > 2 && ZJUSERINFORMODEL.DEPT_MODEL_BOOK.DEPT_DEADDRESS_COORDINATE_Y.length > 2) {
        ZJBookAndJobAnnotation *annotation_book = [[ZJBookAndJobAnnotation alloc] init];
        annotation_book.index = 0;
        annotation_book.coordinate = CLLocationCoordinate2DMake([ZJUSERINFORMODEL.DEPT_MODEL_BOOK.DEPT_DEADDRESS_COORDINATE_X doubleValue], [ZJUSERINFORMODEL.DEPT_MODEL_BOOK.DEPT_DEADDRESS_COORDINATE_Y doubleValue]);
        [self.mapView addAnnotation:annotation_book];
        
    }else{
        self.mapControlView.workAddress.hidden = YES;
    }
}



//活动
- (void)networking_atcit{
    __weak typeof(self) weakSelf = self;
    
    NSDictionary * dict   = @{   @"PAGE": @"1",
                                 @"COUNT": @"100",
                                 @"DEPTID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                                 @"BIG_CLASS": @"",
                                 @"SMALL_CLASS" : @"",
                                 @"USER_ID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                                 @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActListACT_NOT_OVER] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//请求成功
            weakSelf.annotationArray = [[NSArray yy_modelArrayWithClass:[ActivityJoinDetailModel class] json:[object objectForKey:@"ACT_NOT_OVER"]] mutableCopy];
            
        }else{
            
            [weakSelf.annotationArray removeAllObjects];
        }
        [weakSelf annotationArrayUpdate];
    } Fail:^(id object) {
        [weakSelf.annotationArray removeAllObjects];
        [weakSelf annotationArrayUpdate];
    }];
}
//更新坐标点
- (void)annotationArrayUpdate{
    for (QPointAnnotation *annotation in self.mapView.annotations) {
        if ([annotation isKindOfClass:[ZJPointAnnotation class]]) {
            [self.mapView removeAnnotation:annotation];
        }
    }
    
    for (int i = 0; i < self.annotationArray.count; i++) {
        ZJPointAnnotation* anno = [[ZJPointAnnotation alloc] init];
        id model1 = self.annotationArray[0];
       if ([model1 isKindOfClass:[ActivityJoinDetailModel class]]) {//活动
            ActivityJoinDetailModel *model = self.annotationArray[i];
            anno.coordinate = CLLocationCoordinate2DMake([model.POINT_X doubleValue],[model.POINT_Y doubleValue]);
            anno.index = i;
            [self.mapView addAnnotation:anno];
        }
        
    }
}

#pragma mark - ZJPAActionesTableViewDelegate (列表切换成地图)协议方法
- (void)ZJPAActionesTableViewDelegate_viewClick{
    self.mapControlView.hidden = NO;
    self.mapView.hidden = NO;
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.5 animations:^{
        weakSelf.statusVeiw.alpha = 1;
        weakSelf.mapControlView.alpha = 1;
        weakSelf.mapView.alpha = 1;
        weakSelf.shareList.alpha = 0;
        
    } completion:^(BOOL finished) {
         weakSelf.shareList.hidden = NO;
    }];

}
#pragma mark - ZJMapControlViewDelegate 协议方法
//菜单
- (void)ZJMapControlViewDelegate_meunButtonClick{
    self.statusVeiw.hidden = NO;
    self.shareList.alpha = 0;
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.5 animations:^{
        weakSelf.shareList.hidden = NO;
        weakSelf.statusVeiw.alpha = 1;
        weakSelf.mapControlView.alpha = 0;
        weakSelf.mapView.alpha = 0;
        weakSelf.shareList.alpha = 1;
        
    } completion:^(BOOL finished) {
        weakSelf.mapControlView.hidden = YES;
        weakSelf.mapView.hidden = YES;
        [weakSelf.shareList initDataSoure];
    }];
    
}


//当前位置
- (void)ZJMapControlViewDelegate_currentLocationButtonClick{
    if (CenterCoordinate.latitude != 0) {
        [self.mapView setCenterCoordinate:CenterCoordinate  animated:YES];
    }
    _isClickLocation = YES;
    [self.mapView setShowsUserLocation:YES];
}
//在职
- (void)ZJMapControlViewDelegate_homeButtonClick{
    if (ZJUSERINFORMODEL.DEPT_MODEL_JOB.DEPT_DEADDRESS_COORDINATE_X.length > 0) {
        [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake([ZJUSERINFORMODEL.DEPT_MODEL_JOB.DEPT_DEADDRESS_COORDINATE_X doubleValue], [ZJUSERINFORMODEL.DEPT_MODEL_JOB.DEPT_DEADDRESS_COORDINATE_Y doubleValue]) animated:YES];
    }
}
//在册
- (void)ZJMapControlViewDelegate_workAddressButton{
    if (ZJUSERINFORMODEL.DEPT_MODEL_BOOK.DEPT_DEADDRESS_COORDINATE_X.length > 0) {
        [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake([ZJUSERINFORMODEL.DEPT_MODEL_BOOK.DEPT_DEADDRESS_COORDINATE_X doubleValue], [ZJUSERINFORMODEL.DEPT_MODEL_BOOK.DEPT_DEADDRESS_COORDINATE_Y doubleValue])  animated:YES];
    }
}



#pragma mark - QMapViewDelegate 协议方法
//QMapViewDelegate 中的定位回调函数
- (void)mapView:(QMapView *)mapView didUpdateUserLocation:(QUserLocation *)userLocation {
    if (_isClickLocation) {//是点击定位
        
        [self.mapView setCenterCoordinate:userLocation.location  animated:YES];
        _isClickLocation = NO;
    }
    CenterCoordinate = userLocation.location;
}

//设置大头针
- (QAnnotationView *)mapView:(QMapView *)mapView
           viewForAnnotation:(id<QAnnotation>)annotation
{   //设置复用标识
    static NSString *pointReuseIdentifier = @"pointReuseIdentifier";
    QAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIdentifier];
    if (annotationView == nil) {
        annotationView = [[QAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIdentifier];
    }
    //显示气泡
    [annotationView setCanShowCallout:YES];
    
    if ([annotation isKindOfClass:[ZJPointAnnotation class]]) {
        ZJPointAnnotation *annotation1 = annotation;
            [annotationView setImage:[UIImage imageNamed:@"map_huodong"]];
            ActivityJoinDetailModel *markModel  = self.annotationArray[annotation1.index];
            if ([markModel.UNITID isEqualToString:ZJUSERINFORMODEL.DEPT_MODEL_BOOK.ID]) {
                [annotationView setImage:[UIImage imageNamed:@"map_org_huodong"]];
            }
        return annotationView;
    }else if ([annotation isKindOfClass:[ZJBookAndJobAnnotation class]]){
        ZJBookAndJobAnnotation *annotation1 = (ZJBookAndJobAnnotation *)annotation;
        if (annotation1.index == 0) {//在册
            [annotationView setImage:[UIImage imageNamed:@"map_building"]];
        }else{//在职
            if (![[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.AUTH_STATUS isEqualToString:@"1"]) {//审核中
                [annotationView setImage:[UIImage imageNamed:@"map_homne"]];
            }
        }
        return annotationView;
    }
    return nil;
}
//点击大头针走的方法（根据View里面的annotation的index值判断）
- (void)mapView:(QMapView *)mapView didSelectAnnotationView:(QAnnotationView *)view{
    if ([view.annotation isKindOfClass:[ZJPointAnnotation class]]) {
        ZJPointAnnotation *annotation = view.annotation;
            ActivityJoinDetailModel *model = self.annotationArray[annotation.index];
            LPActivityDetailViewController *vc = [LPActivityDetailViewController new];
            vc.model = model;
            [self.navigationController pushViewController:vc animated:YES];
      
    }else if ([view.annotation isKindOfClass:[ZJBookAndJobAnnotation class]]){
        ZJBookAndJobAnnotation *annotation = view.annotation;
        if (annotation.index == 0) {//在册
            BranchPartyOrgizationModel *model = [BranchPartyOrgizationModel yy_modelWithJSON:[ZJUSERINFORMODEL.DEPT_MODEL_BOOK yy_modelToJSONObject]];
            
            BranchOpenPartyOrganizationViewController *vc = [BranchOpenPartyOrganizationViewController new];
            vc.model = model;
            [self.navigationController pushViewController:vc animated:YES];
        }else{//在职
            BranchPartyOrgizationModel *model = [BranchPartyOrgizationModel yy_modelWithJSON:[ZJUSERINFORMODEL.DEPT_MODEL_JOB yy_modelToJSONObject]];
            
            BranchOpenPartyOrganizationViewController *vc = [BranchOpenPartyOrganizationViewController new];
            vc.model = model;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    //选中状态设置NO。可以连续点击
    [view setSelected:NO];
}



#pragma mark - Setter


#pragma mark - Getters

- (QMapView *)mapView{
    if (_mapView == nil){
        _mapView = [[QMapView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.view.height - LP_TopHeight)];
        _mapView.showsUserLocation = YES;//显示用户位置
        _mapView.showsCompass = NO;//不显示指南针
        _mapView.userTrackingMode = QMUserTrackingModeFollow;
        _mapView.zoomLevel = 16;
        _mapView.delegate = self;
        _mapView.disable3DBuildingEffect = YES;
        
    }
    return _mapView;
}
- (ZJMapControlView *)mapControlView{
    if (_mapControlView == nil){
        _mapControlView = [[ZJMapControlView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.view.height - LP_TopHeight)];
        _mapControlView.delegate = self;
        _mapControlView.organizationButton.hidden = YES;
        _mapControlView.activityButton.hidden = YES;
        _mapControlView.positionButton.hidden = YES;
    }
    return _mapControlView;
}

- (NSMutableArray *)annotationArray{
    if (_annotationArray == nil){
        _annotationArray = [NSMutableArray array];
    }
    return _annotationArray;
}
- (ZJPAActionesTableView *)shareList{
    if (_shareList == nil){
        _shareList = [[ZJPAActionesTableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.view.height - LP_TopHeight)];
        _shareList.delegate = self;
        
    }
    return _shareList;
}

@end
